package comp512.classes;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

public class ServerHandlerThread extends Thread{
	
	private Message message;
	private Socket socket;
	private Server server;
	
	public ServerHandlerThread(Message message, Socket socket) throws IOException {
		this.message = message;
		this.server = Server.getInstance();
		this.socket = socket;
	}
	
	private void handleMessage() throws IOException {
		switch (message.getFlag()) {
		
		case Flags.TEAM_MESSAGE_FLAG:
			
			String playerSessionId = message.getPlayer().getSessionId();
			String playerTeamName = message.getPlayer().getTeamName();
			String teamMessageToSend = message.getMessage();
			
			List<Player> playersInSession = new ArrayList<>();
			synchronized (server.getActiveSessionPlayers()) {
				playersInSession = server.getActiveSessionPlayers().get(playerSessionId);	
			}
			for(Player player: playersInSession){
				if(!player.equals(message.getPlayer()) && playerTeamName.equals(message.getPlayer().getTeamName())){
					Message messageToSend = new Message(Flags.RECEIVE_TEAM_MESSAGE_FLAG, player, teamMessageToSend);
					ObjectOutputStream playerOutputStream = new ObjectOutputStream(player.getSocket().getOutputStream());
					playerOutputStream.writeObject(message.getPlayer().getPlayerId() + ": " + messageToSend);
				}
			}

			break;
			
		case Flags.ANSWER_MESSAGE_FLAG:
			//TODO
			break;
			
		case Flags.GLOBAL_MESSAGE_FLAG:
			
			playerSessionId = message.getPlayer().getSessionId();
			String globalMessageToSend = message.getMessage();
			synchronized (server.getActiveSessionPlayers()) {
				playersInSession = server.getActiveSessionPlayers().get(playerSessionId);
			}
			for(Player player: playersInSession){
				Message messageToSend = new Message(Flags.RECEIVE_GLOBAL_MESSAGE_FLAG, player, globalMessageToSend);
				ObjectOutputStream playerOutputStream = new ObjectOutputStream(player.getSocket().getOutputStream());
				playerOutputStream.flush();
				playerOutputStream.writeObject(message.getPlayer().getPlayerId() + ": " + messageToSend);
				playerOutputStream.flush();
				playerOutputStream.close();
			}
			break;
			
		case Flags.INVITE_FRIEND_FLAG:
			//TODO
			break;
			
		case Flags.LOGIN_MESSAGE_FLAG:
			
			ObjectOutputStream objectOutputStream = null;
			try {
				Player player = message.getPlayer();
				Player savedPlayer = null;
				savedPlayer = server.getSavedPlayer(player);
				player.setSocket(socket);
				if(savedPlayer != null){
					player.setStatistics(savedPlayer.getStatistics());
				} else{
					player.setStatistics(new Statistics());
				}
				synchronized (server.getActiveSessionPlayers()) {
					server.addPlayerToLobby(player);					
				}
				objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
				objectOutputStream.reset();
				Message messageToSend = new Message(Flags.RECEIVE_NEW_SESSION_FLAG, player, "Login Successful");
				objectOutputStream.writeObject(messageToSend);
				objectOutputStream.flush();
				objectOutputStream.close();
			} catch (SocketException e) {
				//TODO
				e.printStackTrace();
			} catch (IOException e) {
				//TODO
				e.printStackTrace();
			}
			break;
			
		case Flags.DISCONNECT_MESSAGE_FLAG:
			//TODO
			break;
			
		case Flags.CREATE_NEW_GAME_FLAG:
			//TODO
			break;
			
		case Flags.JOIN_SELECTED_GAME_FLAG:
			//TODO
			break;
			
		default:
			// TODO
			break;
			
		}
	}

	@Override
	public void run() {
		try {
			handleMessage();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
